package com.fingard.dsp.bank.directbank.lchs01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;

/**
 * 创兴银行-明细查询
 * @author alan
 * @date 2020-06-04
 * @version 1.0
 */
public class LCHS01Transaction extends LCHS01Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());
            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());
            //按自然月循环查询
            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            while (tmpIntDateFrom <= tmpIntDateTo) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否跨月
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    //跨月则设置为月末
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }
                //赋值通用参数
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                LCHS01Header bankHeader = new LCHS01Header();
                bankHeader.trCode = "0002";
                bankHeader.entSeqNo = p_req7516.reqHeader.transSeqID;
                writeCommonHeader(xmlWriter, bankHeader);
                XElement tmpXElement = new XElement("body", "");
                tmpXElement.addChild(new XElement("accNo", p_req7516.actNum));//账号
                tmpXElement.addChild(new XElement("cry", convertCurToBank(p_req7516.actCur)));
                tmpXElement.addChild(new XElement("tranStartDate", Format.Date8Format.format(tmpCalendarFrom.getTime())));//查询起始日期
                tmpXElement.addChild(new XElement("tranEndDate", Format.Date8Format.format(tmpClndrStepTo.getTime())));//查询结束日期
                xmlWriter.writeXElement(tmpXElement);
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), p_req7516.reqHeader.transType, p_req7516.reqHeader.transType);
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/ifp/body/errorCode");
                        String returnMsg = tmpBkReader.getFirstText("/ifp/body/errorMsg");
                        if ("0".equals(returnCode)) {
                            while (tmpBkReader.read("/ifp/body/list/coll")){
                                RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                tmpRetDetail.actArea = p_req7516.actArea;//银行地区区域码
                                /**银行账号*/
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("acctNo");
                                /**币种*/
                                tmpRetDetail.cur = p_req7516.actCur;
                                /**对方账号*/
                                tmpRetDetail.recAccNum = tmpBkReader.readedXElement.getSingleTextNotNull("qteAcctTPNo");
                                /**对方户名*/
                                tmpRetDetail.recAccName = tmpBkReader.readedXElement.getSingleTextNotNull("reciprocalAccoName");
                                tmpRetDetail.tranDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("tranDate"));
                                String tranTime = tmpBkReader.readedXElement.getSingleTextNotNull("tranTimeStamp");
                                tmpRetDetail.tranTime=tmpRetDetail.tranDate+" "+tranTime;
                                /**起息日期*/
                                tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                String tranAmt=tmpBkReader.readedXElement.getSingleTextNotNull("tranAMT");
                                /**D:借,C:贷*/
                                String cdFlag=tmpBkReader.readedXElement.getSingleTextNotNull("dbInd");
                                if(tranAmt.startsWith("-")){
                                    /**借贷标志*/
                                    if ("C".equals(cdFlag)) {
                                        tmpRetDetail.cdFlag = "1";
                                    } else if ("D".equals(cdFlag)) {
                                        tmpRetDetail.cdFlag = "2";
                                    }
                                    /**交易金额*/
                                    tmpRetDetail.tranAmt =tranAmt.substring(1);
                                }else {
                                    /**借贷标志*/
                                    if ("C".equals(cdFlag)) {
                                        tmpRetDetail.cdFlag = "2";
                                    } else if ("D".equals(cdFlag)) {
                                        tmpRetDetail.cdFlag = "1";
                                    }
                                    /**交易金额*/
                                    tmpRetDetail.tranAmt =tranAmt;
                                }
                                /**账号余额*/
                                tmpRetDetail.accBal = tmpBkReader.readedXElement.getSingleTextNotNull("acctBal");
                                /**用途(摘要)*/
                                tmpRetDetail.usage = tmpBkReader.readedXElement.getSingleTextNotNull("tranDesc");
                                /**对账码*/
                                tmpRetDetail.abstr = pickupAbstr(tmpRetDetail.usage);
                                /**描述信息*/
                                tmpRetDetail.comment = tmpBkReader.readedXElement.getSingleTextNotNull("descMsg");
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        } else if("000305".equals(returnCode)){
                            tmpRespCode = "0";
                            tmpRespInfo = returnMsg;
                        }else {
                            tmpRespCode = "-1";
                            tmpRespInfo = returnMsg;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);//下个月
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
            retAts.writeEnd();
            return retAts;
        }
        if ("-1".equals(tmpRespCode)) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空!");
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    //当日明细查询调用历史明细查询方法
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        //通用b2e0035
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actArea = p_req7515.actArea;
        tmpReq7516.actCur = p_req7515.actCur;
        tmpReq7516.actNum = p_req7515.actNum;
        tmpReq7516.higherLevelAct = p_req7515.higherLevelAct;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;
        tmpReq7516.dateTo = tmpStrToday;
        return d_7516_HisTrans(tmpReq7516);
    }
}
